﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Windows;
using System.Xml.Serialization;
using System.IO;
using BDSim.用户界面.Models;

namespace BDSim
{
    public partial class MainWindowViewModel : PropertyNotify
    {
        public MainWindowViewModel()
        {
            RegisterCommandEvent();
            BDSimObject.DlgReportSimInfo = SimInfoReport;
            BDSimObject.DlgReportPropertyInfo = PropertyInfoReport;
            MainDispatcher.StartPath = Directory.GetCurrentDirectory();
            ReadPluginFile();
        }

        #region 仿真场景模型
        Scenario _Scenario;

        public Scenario Scenario
        {
            get { return _Scenario; }
            set { _Scenario = value; NotifyPropertyChanged("Scenario"); }
        }

        #endregion

        #region 父级窗体
        private Window _Parent;
        [XmlIgnore]
        public Window Parent
        {
            get { return _Parent; }
            set { _Parent = value; BindingRoutedCommand(); DefineMenuBarItem(); }
        }
        #endregion

        #region BDSim系统设置
        BDSimSetting _SystemSetting;
        [XmlIgnore]
        public BDSimSetting SystemSetting
        {
            get { return _SystemSetting; }
            set { _SystemSetting = value; }
        }

        ObservableCollection<Plugin> _PluginList = new ObservableCollection<Plugin>();

        public ObservableCollection<Plugin> PluginList
        {
            get { return _PluginList; }
            set { _PluginList = value; NotifyPropertyChanged("PluginList"); }
        } 
        #endregion

        #region 系统消息模型

        private ObservableCollection<SimInfo> _SysReportList = new ObservableCollection<SimInfo>();
        /// <summary>
        /// 系统消息报告列表
        /// </summary>
        [XmlIgnore]
        public ObservableCollection<SimInfo> SysReportList
        {
            get { return _SysReportList; }
            set { _SysReportList = value; NotifyPropertyChanged("SysReportList"); }
        }

        #endregion

        #region //输出系统消息、报告

        /// <summary>
        /// 报告仿真消息
        /// </summary>
        /// <param name="msg"></param>
        void SimInfoReport(SimInfo msg)
        {
            MainDispatcher.MainWindowDispatcher.BeginInvoke(new Action(() =>
                    {
                        SysReportList.Add(msg);
                    }));
        }

        /// <summary>
        /// 报告属性设置的错误信息
        /// </summary>
        /// <param name="str"></param>
        void PropertyInfoReport(string str)
        {
            MessageBox.Show(str, "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
        }
        #endregion

        #region 场景名称

        private string _Name = "BDSim";

        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }
        #endregion

        void ReadPluginFile()
        {
            if (File.Exists("pluginList.xml"))
            {
                PluginList = XmlFileOperater<ObservableCollection<Plugin>>.LoadFromFile("pluginList.xml");
            }
        }

        ~MainWindowViewModel()
        {

        }
    }
}
